package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.YjhApplication;
import com.hxkcloud.patient.adapter.ArticalListAdapter;
import com.hxkcloud.patient.adapter.IvgTextAdapter;
import com.hxkcloud.patient.adapter.KindApater;
import com.hxkcloud.patient.beans.ArticleBean;
import com.hxkcloud.patient.beans.ArticleKindBean;
import com.hxkcloud.patient.beans.ArticleKindChildBean;
import com.hxkcloud.patient.beans.ArticleWholeBean;
import com.hxkcloud.patient.dborm.ArticleKindDao;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/15.
 */
public class AddArticalActivity extends BaseActivity implements View.OnClickListener, PullToRefreshLayout.OnRefreshListener, ArticalListAdapter.ResultSelectedListener, IvgTextAdapter.ResultSelectedListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_sure;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<ArticleBean> articleBeanList;
    private ArticalListAdapter adapter;

    private RecyclerView mRecyclerView;
    private IvgTextAdapter mAdapter;
    private List<ArticleKindBean> kindNews;//分类

    private LinearLayout kindLayout;
    private TextView kind1TV;
    private TextView kind2TV;
    private TextView kind3TV;
    private TextView kind4TV;
    private ImageView kind1IV;
    private ImageView kind2IV;
    private ImageView kind3IV;
    private ImageView kind4IV;
    private LinearLayout ll_kind1;
    private LinearLayout ll_kind2;
    private LinearLayout ll_kind3;
    private LinearLayout ll_kind4;

    private PopupWindow kindPopup;//选择分类
    private ListView kindListView;
    private KindApater kindAdapter;

    private View kindWholeView;

    private ArticleWholeBean resultBean;

    private List<ArticleKindChildBean> kindChilds;

    private int pageIndex = 1;
    private int pageSize = 15;

    private boolean isLoadMore = false;// 是否加载更多

    private int totalCount = 0;

    private boolean isChoose = false;

    private boolean isKind1 = false;
    private boolean isKind2 = false;
    private boolean isKind3 = false;
    private boolean isKind4 = false;

    private boolean[] isKinds;

    private List<ArticleBean> chooseArticles;//记录选择的文章

    private boolean isChoosed1 = false;
    private boolean isChoosed2 = false;
    private boolean isChoosed3 = false;
    private boolean isChoosed4 = false;

    private int curTitle = -1;

    private String selectChildId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_addartical);
        articleBeanList = new ArrayList<ArticleBean>();
        chooseArticles = new ArrayList<>();
        initView();
        initData();
        initListener();
    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_ARTICLELIST_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ArticleWholeBean) msg.obj;
                    totalCount = resultBean.getTotalCount();
                    if (resultBean.getList() != null) {

                        if (resultBean.getList().size() == 0) {
                            if (isLoadMore) {
                                refreshLayout
                                        .loadmoreFinish(PullToRefreshLayout.SUCCEED);
                            } else {
                                articleBeanList.clear();
                                adapter.setData(articleBeanList);
                                refreshLayout
                                        .refreshFinish(PullToRefreshLayout.SUCCEED);
                            }
                        } else {
                            if (isLoadMore) {

                            } else {
                                articleBeanList.clear();
                            }
                            articleBeanList.addAll(resultBean.getList());
                            //判断已选择的
                            for (ArticleBean article : articleBeanList) {
                                for (ArticleBean select : chooseArticles) {
                                    if (article.getArticleId().equals(select.getArticleId())) {
                                        article.setIsSelect(true);
                                        break;
                                    }
                                }
                            }
                            adapter.setData(articleBeanList);
                            if (isLoadMore) {
                                refreshLayout
                                        .loadmoreFinish(PullToRefreshLayout.SUCCEED);
                            } else {
                                refreshLayout
                                        .refreshFinish(PullToRefreshLayout.SUCCEED);
                            }

                        }

                    } else {
                        articleBeanList.clear();
                        adapter.setData(articleBeanList);
                    }
                }

                break;
            case MessageCode.NETWORKCODE_ARTICLELIST_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;

        }

    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_sure = (TextView) findViewById(R.id.tv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.notice));
        tv_sure.setVisibility(View.VISIBLE);
        tv_sure.setText(getResources().getText(R.string.sure));
        iv_back.setOnClickListener(this);
        tv_sure.setOnClickListener(this);

        kindLayout = (LinearLayout) findViewById(R.id.addartical_layout_kind);
        kind1TV = (TextView) findViewById(R.id.addartical_tv_kind1);
        kind2TV = (TextView) findViewById(R.id.addartical_tv_kind2);
        kind3TV = (TextView) findViewById(R.id.addartical_tv_kind3);
        kind4TV = (TextView) findViewById(R.id.addartical_tv_kind4);
        kind1IV = (ImageView) findViewById(R.id.addartical_iv_kind1);
        kind2IV = (ImageView) findViewById(R.id.addartical_iv_kind2);
        kind3IV = (ImageView) findViewById(R.id.addartical_iv_kind3);
        kind4IV = (ImageView) findViewById(R.id.addartical_iv_kind4);
        ll_kind1 = (LinearLayout) findViewById(R.id.addartical_layout_kind1);
        ll_kind2 = (LinearLayout) findViewById(R.id.addartical_layout_kind2);
        ll_kind3 = (LinearLayout) findViewById(R.id.addartical_layout_kind3);
        ll_kind4 = (LinearLayout) findViewById(R.id.addartical_layout_kind4);

        refreshLayout = (PullToRefreshLayout) findViewById(R.id.addartical_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.addartical_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);

        adapter = new ArticalListAdapter(this, articleBeanList, recycleView, this, screenWidth);
        recycleView.setAdapter(adapter);

        mRecyclerView = (RecyclerView) findViewById(R.id.addartical_recyclerview_horizontal);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);


        initKindPopup();

    }

    private void initKindPopup() {
        kindPopup = new PopupWindow(this);
        View kindView = LayoutInflater.from(this).inflate(R.layout.popup_layout_kind, null);
        kindPopup.setContentView(kindView);

        kindPopup.setWidth(screenWidth);
        kindPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        kindPopup.setFocusable(true);
        kindPopup.setTouchable(true);
        kindPopup.setOutsideTouchable(true);
        kindPopup.setBackgroundDrawable(new ColorDrawable(0x00000000));


        if (kindChilds == null) {
            kindChilds = new ArrayList<ArticleKindChildBean>();
        } else {
            kindChilds.clear();
        }


        kindListView = (ListView) kindView.findViewById(R.id.kind_listview);
        kindAdapter = new KindApater(this, kindChilds);
        kindListView.setAdapter(kindAdapter);
        kindListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < kindChilds.size(); i++) {
                    if (i == position) {
                        kindChilds.get(i).setIsChoosed(true);
                        selectChildId = kindChilds.get(position).getItemId();
                    } else {
                        kindChilds.get(i).setIsChoosed(false);
                    }
                }
                if (isKind1) {
                    isChoosed1 = true;
                    isChoosed2 = false;
                    isChoosed3 = false;
                    isChoosed4 = false;
                    kind1IV.setImageResource(R.mipmap.tab_arrow_s);
                    kind2IV.setImageResource(R.mipmap.tab_arrow);
                    kind3IV.setImageResource(R.mipmap.tab_arrow);
                    kind4IV.setImageResource(R.mipmap.tab_arrow);
                    kind1TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                    kind2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                } else if (isKind2) {
                    isChoosed1 = false;
                    isChoosed2 = true;
                    isChoosed3 = false;
                    isChoosed4 = false;
                    kind1IV.setImageResource(R.mipmap.tab_arrow);
                    kind2IV.setImageResource(R.mipmap.tab_arrow_s);
                    kind3IV.setImageResource(R.mipmap.tab_arrow);
                    kind4IV.setImageResource(R.mipmap.tab_arrow);
                    kind1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind2TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                    kind3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                } else if (isKind3) {
                    isChoosed1 = false;
                    isChoosed2 = false;
                    isChoosed3 = true;
                    isChoosed4 = false;
                    kind1IV.setImageResource(R.mipmap.tab_arrow);
                    kind2IV.setImageResource(R.mipmap.tab_arrow);
                    kind3IV.setImageResource(R.mipmap.tab_arrow_s);
                    kind4IV.setImageResource(R.mipmap.tab_arrow);
                    kind1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind3TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                    kind4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                } else if (isKind4) {
                    isChoosed1 = false;
                    isChoosed2 = false;
                    isChoosed3 = false;
                    isChoosed4 = true;
                    kind1IV.setImageResource(R.mipmap.tab_arrow);
                    kind2IV.setImageResource(R.mipmap.tab_arrow);
                    kind3IV.setImageResource(R.mipmap.tab_arrow);
                    kind4IV.setImageResource(R.mipmap.tab_arrow_s);
                    kind1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind4TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                }

                if (kindNews.size() > 4) {
                    for (int i = 0; i < isKinds.length; i++) {
                        if (isKinds[i]) {
                            curTitle = i;
                            break;
                        }
                    }
                }
                loading();
                YiJiaHuNetWorkUtils.articleList(loginInfo.getDepartmentId(), "", kindChilds.get(position).getItemId(), YjhApplication.articlekind_course, pageIndex, pageSize, handler);
                kindPopup.dismiss();
            }
        });

        kindWholeView = (View) kindView.findViewById(R.id.kind_view_whole);
        kindWholeView.setOnClickListener(this);

        kindPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (kindNews.size() > 4) {
                    if(curTitle==-1){
                        for (int i = 0; i < kindNews.size(); i++) {
                            kindNews.get(i).setIsSelected(false);
                        }
                    }else{
                        for (int i = 0; i < kindNews.size(); i++) {
                            if(i==curTitle){
                                kindNews.get(curTitle).setIsSelected(true);
                            }else{
                                kindNews.get(i).setIsSelected(false);
                            }

                        }

                    }
                    mAdapter.setData(kindNews);
                }

                if (isChoosed1) {
                    if (isKind1) {
                        kind1TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                        kind1IV.setImageResource(R.mipmap.tab_arrow_s);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }
                } else {
                    if (isKind1) {
                        kind1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }

                }

                if (isChoosed2) {
                    if (isKind2) {
                        kind2TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow_s);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }
                } else {
                    if (isKind2) {
                        kind2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }

                }

                if (isChoosed3) {
                    if (isKind3) {
                        kind3TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow_s);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }
                } else {
                    if (isKind3) {
                        kind3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }

                }

                if (isChoosed4) {
                    if (isKind4) {
                        kind4TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow_s);
                    }
                } else {
                    if (isKind4) {
                        kind4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }

                }


            }
        });
    }

    @Override
    protected void initListener() {

        iv_back.setOnClickListener(this);
        tv_sure.setOnClickListener(this);

        kind1TV.setOnClickListener(this);
        kind2TV.setOnClickListener(this);
        kind3TV.setOnClickListener(this);
        kind4TV.setOnClickListener(this);

        refreshLayout.setOnRefreshListener(this);


    }

    @Override
    protected void initData() {
        ArticleKindDao kindDao = new ArticleKindDao(mContext);
        kindNews = kindDao.getKinds(loginInfo.getDepartmentId(), YjhApplication.articlekind_course);

        if (kindNews.size() == 0) {
            kindLayout.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.GONE);
        }
        if (kindNews.size() < 5) {
            kindLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
            ll_kind1.setVisibility(View.GONE);
            ll_kind2.setVisibility(View.GONE);
            ll_kind3.setVisibility(View.GONE);
            ll_kind4.setVisibility(View.GONE);
            switch (kindNews.size()) {
                case 4:
                    ll_kind4.setVisibility(View.VISIBLE);
                    kind4TV.setText(kindNews.get(3).getName());
                case 3:
                    ll_kind3.setVisibility(View.VISIBLE);
                    kind3TV.setText(kindNews.get(2).getName());
                case 2:
                    ll_kind2.setVisibility(View.VISIBLE);
                    kind2TV.setText(kindNews.get(1).getName());
                case 1:
                    ll_kind1.setVisibility(View.VISIBLE);
                    kind1TV.setText(kindNews.get(0).getName());
                    break;
            }

        } else {
            kindLayout.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);

            //设置适配器
            isKinds = new boolean[kindNews.size()];
            for (int i = 0; i < kindNews.size(); i++) {
                isKinds[i] = false;
                kindNews.get(i).setIsSelected(false);
            }
            mAdapter = new IvgTextAdapter(this, kindNews, this);
            mRecyclerView.setAdapter(mAdapter);

        }
        loading();
        YiJiaHuNetWorkUtils.articleList(loginInfo.getDepartmentId(), "", "", YjhApplication.articlekind_course, pageIndex, pageSize, handler);
    }

    private void initKind() {
        kind1TV.setTextColor(getResources().getColor(R.color.grey_text1));
        kind2TV.setTextColor(getResources().getColor(R.color.grey_text1));
        kind3TV.setTextColor(getResources().getColor(R.color.grey_text1));
        kind4TV.setTextColor(getResources().getColor(R.color.grey_text1));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.tv_title_func:
//                toastMsg(chooseArticles.size() + " num");
                Intent iBack = new Intent();
                String[] ids = new String[chooseArticles.size()];
                String[] names = new String[chooseArticles.size()];
                for (int i = 0; i < chooseArticles.size(); i++) {
                    ids[i] = chooseArticles.get(i).getArticleId();
                    names[i] = chooseArticles.get(i).getName();
                }
                iBack.putExtra("ids", ids);
                iBack.putExtra("names", names);
                setResult(RESULT_OK, iBack);
                finish();
                break;
            case R.id.addartical_tv_kind1:

                kind1IV.setImageResource(R.mipmap.tab_arrow_pre);
                kind2IV.setImageResource(R.mipmap.tab_arrow);
                kind3IV.setImageResource(R.mipmap.tab_arrow);
                kind4IV.setImageResource(R.mipmap.tab_arrow);
                kind1TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                kind2TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));
                kind3TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));
                kind4TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));

                if (isKind1) {
                    isKind2 = false;
                    isKind3 = false;
                    isKind4 = false;
                } else {
                    getKindChilds(0);
                }
                if(kindChilds!=null){
                    for (int i = 0; i < kindChilds.size(); i++) {
                        if (selectChildId.equals(kindChilds.get(i).getItemId())) {
                            kindChilds.get(i).setIsChoosed(true);
                        } else {
                            kindChilds.get(i).setIsChoosed(false);
                        }
                    }
                }
                kindAdapter.setData(kindChilds);
                kindPopup.showAsDropDown(kindLayout);
                break;
            case R.id.addartical_tv_kind2:
                kind1IV.setImageResource(R.mipmap.tab_arrow);
                kind2IV.setImageResource(R.mipmap.tab_arrow_pre);
                kind3IV.setImageResource(R.mipmap.tab_arrow);
                kind4IV.setImageResource(R.mipmap.tab_arrow);

                kind1TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));
                kind2TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                kind3TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));
                kind4TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));

                if (isKind2) {
                    isKind1 = false;
                    isKind3 = false;
                    isKind4 = false;
                } else {
                    getKindChilds(1);
                }
                if(kindChilds!=null){
                    for (int i = 0; i < kindChilds.size(); i++) {
                        if (selectChildId.equals(kindChilds.get(i).getItemId())) {
                            kindChilds.get(i).setIsChoosed(true);
                        } else {
                            kindChilds.get(i).setIsChoosed(false);
                        }
                    }
                }
                kindAdapter.setData(kindChilds);
                kindPopup.showAsDropDown(kindLayout);
                break;
            case R.id.addartical_tv_kind3:
                kind1IV.setImageResource(R.mipmap.tab_arrow);
                kind2IV.setImageResource(R.mipmap.tab_arrow);
                kind3IV.setImageResource(R.mipmap.tab_arrow_pre);
                kind4IV.setImageResource(R.mipmap.tab_arrow);
                kind1TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));
                kind2TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));
                kind3TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                kind4TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));

                if (isKind3) {
                    isKind1 = false;
                    isKind2 = false;
                    isKind4 = false;
                } else {
                    getKindChilds(2);
                }
                if(kindChilds!=null){
                    for (int i = 0; i < kindChilds.size(); i++) {
                        if (selectChildId.equals(kindChilds.get(i).getItemId())) {
                            kindChilds.get(i).setIsChoosed(true);
                        } else {
                            kindChilds.get(i).setIsChoosed(false);
                        }
                    }
                }
                kindAdapter.setData(kindChilds);
                kindPopup.showAsDropDown(kindLayout);
                break;
            case R.id.addartical_tv_kind4:
                kind1IV.setImageResource(R.mipmap.tab_arrow);
                kind2IV.setImageResource(R.mipmap.tab_arrow);
                kind3IV.setImageResource(R.mipmap.tab_arrow);
                kind4IV.setImageResource(R.mipmap.tab_arrow_pre);
                kind1TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));
                kind2TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));
                kind3TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));
                kind4TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));

                if (isKind4) {
                    isKind1 = false;
                    isKind3 = false;
                    isKind2 = false;
                } else {
                    getKindChilds(3);
                }
                if(kindChilds!=null){
                    for (int i = 0; i < kindChilds.size(); i++) {
                        if (selectChildId.equals(kindChilds.get(i).getItemId())) {
                            kindChilds.get(i).setIsChoosed(true);
                        } else {
                            kindChilds.get(i).setIsChoosed(false);
                        }
                    }
                }
                kindAdapter.setData(kindChilds);
                kindPopup.showAsDropDown(kindLayout);
                break;
            case R.id.kind_view_whole:
                if (kindPopup != null) {
                    kindPopup.dismiss();


                }

        }

    }

    /**
     *
     * @param postion
     */
    public void getKindChilds(int postion) {
        switch (postion) {
            case 0:
                isKind1 = true;
                isKind4 = false;
                isKind3 = false;
                isKind2 = false;
                break;
            case 1:
                isKind2 = true;
                isKind4 = false;
                isKind3 = false;
                isKind1 = false;
                break;
            case 2:
                isKind3 = true;
                isKind4 = false;
                isKind2 = false;
                isKind1 = false;
                break;
            case 3:
                isKind4 = true;
                isKind3 = false;
                isKind2 = false;
                isKind1 = false;
                break;
        }
        kindChilds.clear();
//        ArticleKindDao kindDao = new ArticleKindDao(mContext);
//        kindNews = kindDao.getKinds(loginInfo.getDepartmentId(), YjhApplication.articlekind_course);
        ArticleKindChildBean bean = new ArticleKindChildBean();
        bean.setName(getResources().getString(R.string.usermanger_text_all));
        bean.setItemId(kindNews.get(postion).getItemId());
        kindChilds.add(bean);
        kindChilds.addAll(kindNews.get(postion).getChildList());
    }


    @Override
    public void onSelectedArtical(String type, int position) {
        if ("select".equals(type)) {
            articleBeanList.get(position).setIsSelect(true);
            adapter.notifyItemChanged(position);
            chooseArticles.add(articleBeanList.get(position));
        } else if ("no_select".equals(type)) {
            articleBeanList.get(position).setIsSelect(false);
            adapter.notifyItemChanged(position);
            for (int i = 0; i < chooseArticles.size(); i++) {
                ArticleBean article = chooseArticles.get(i);
                if (article.getArticleId().equals(articleBeanList.get(position).getArticleId())) {
                    chooseArticles.remove(i);
                }
            }
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        isLoadMore = false;
        pageIndex = 1;
        articleBeanList.clear();
        loading();
        YiJiaHuNetWorkUtils.articleList(loginInfo.getDepartmentId(), "", "", YjhApplication.articlekind_course, pageIndex, pageSize, handler);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

        if (articleBeanList.size() == totalCount) {
            isLoadMore = false;
            refreshLayout
                    .loadmoreFinish(PullToRefreshLayout.SUCCEED);
            return;
        }

        isLoadMore = true;
        pageIndex++;
        loading();
        YiJiaHuNetWorkUtils.articleList(loginInfo.getDepartmentId(), "", "", YjhApplication.articlekind_course, pageIndex, pageSize, handler);

    }

    @Override
    public void onSelected(String type, int position) {

        for (int i = 0; i < kindNews.size(); i++) {
            if (i == position) {
                kindNews.get(position).setIsSelected(true);
            } else {
                kindNews.get(i).setIsSelected(false);
            }
        }
        mAdapter.setData(kindNews);

        if (isKinds[position]) {
            for (int i = 0; i < isKinds.length; i++) {
                if (i != position) {
                    isKinds[i] = false;
                }
            }
        } else {
            for (int i = 0; i < isKinds.length; i++) {
                isKinds[i] = false;
            }
            isKinds[position] = true;
            kindChilds.clear();
//            ArticleKindDao kindDao = new ArticleKindDao(mContext);
//            kindNews = kindDao.getKinds(loginInfo.getDepartmentId(), YjhApplication.articlekind_course);
            ArticleKindChildBean bean = new ArticleKindChildBean();
            bean.setName(getResources().getString(R.string.usermanger_text_all));
            bean.setItemId(kindNews.get(position).getItemId());
            kindChilds.add(bean);
            kindChilds.addAll(kindNews.get(position).getChildList());

        }
        if(kindChilds!=null){
            for (int i = 0; i < kindChilds.size(); i++) {
                if (selectChildId.equals(kindChilds.get(i).getItemId())) {
                    kindChilds.get(i).setIsChoosed(true);
                } else {
                    kindChilds.get(i).setIsChoosed(false);
                }
            }
        }
        kindAdapter.setData(kindChilds);
        kindPopup.showAsDropDown(mRecyclerView);

    }
}
